package net.cl.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间日期处理工具类
 * 
 * @author CaoLei
 */
public class DateUtil {

	public static final String[] WEEK_CHN = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
	public static final String DEFAULT_FORMAT = "yyyy-MM-dd";
	public static final String FORMAT_TO_SECOND = "yyyy-MM-dd HH:mm:ss";
	public static final int[] DAYS_IN_MONTHS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	public static void main(String[] args) {
		System.out.println(week());
	}

	/**
	 * 获取当前系统时间
	 */
	public static String now() {
		return format(new Date(), FORMAT_TO_SECOND);
	}

	/**
	 * 获得今天的日期(yyyy-MM-dd)
	 */
	public static String today() {
		return format(new Date(), DEFAULT_FORMAT);
	}

	/**
	 * 格式化当前系统时间
	 */
	public static String now(String format) {
		return format(new Date(), format);
	}

	public static String week() {
		return week(new Date());
	}
	
	public static String week(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.setFirstDayOfWeek(Calendar.SUNDAY);
		return WEEK_CHN[c.get(Calendar.DAY_OF_WEEK) - 1];
	}

	/**
	 * 将字符串转化为日期<br>
	 * 默认格式为yyyy-MM-dd
	 */
	public static Date parse(String strDate) {
		return parse(strDate, DEFAULT_FORMAT);
	}

	/**
	 * 将字符串转化为日期
	 */
	public static Date parse(String strDate, String format) {
		if (strDate == null || format == null)
			throw new IllegalArgumentException("The parameter must not be null");
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 将日期按照一定格式转化为字符串
	 */
	public static String format(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date == null || format == null)
			throw new IllegalArgumentException("The parameter must not be null");
		try {
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 比较两个日期间隔的天数
	 */
	public static int getDaysDistance(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			throw new IllegalArgumentException("The date must not be null");
		long dis = date2.getTime() - date1.getTime();
		int days = (int) Math.ceil(dis / 86400000d);
		return days;
	}

	/**
	 * 比较两个日期间隔的小时数
	 */
	public static int getHoursDistance(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			throw new IllegalArgumentException("The date must not be null");
		long dis = date2.getTime() - date1.getTime();
		int hours = (int) Math.ceil(dis / 3600000d);
		return hours;
	}

	/**
	 * 返回如(2000-01-10 12:00:20)
	 */
	public static String toSecond(Date date) {
		return format(date, FORMAT_TO_SECOND);
	}

	/**
	 * 返回如(2000-01-10 12:00)
	 */
	public static String toMinute(Date date) {
		return format(date, "yyyy-MM-dd HH:mm");
	}

	/**
	 * 返回如(2000-01-10)
	 */
	public static String toDay(Date date) {
		return format(date, DEFAULT_FORMAT);
	}

	/**
	 * 返回如(2000-01)
	 */
	public static String toMonth(Date date) {
		return format(date, "yyyy-MM");
	}

	/**
	 * 返回年
	 */
	public static String toYear(Date date) {
		return format(date, "yyyy");
	}

	/**
	 * 判断是否同一天
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			return false;
		return truncateToDay(date1).getTime() == truncateToDay(date2).getTime();
	}

	/**
	 * 判断是否同一时刻
	 */
	public static boolean isSameInstant(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			return false;
		return date1.getTime() == date2.getTime();
	}

	/**
	 * 截断日期天以后的精度
	 */
	public static Date truncateToDay(Date date) {
		date = truncateToHour(date);
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.set(Calendar.HOUR_OF_DAY, 0);
		return gc.getTime();
	}

	/**
	 * 截断日期小时以后的精度
	 */
	public static Date truncateToHour(Date date) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.set(Calendar.MINUTE, 0);
		gc.set(Calendar.SECOND, 0);
		gc.set(Calendar.MILLISECOND, 0);
		return gc.getTime();
	}

	/**
	 * 截断日期分以后的精度
	 */
	public static Date truncateToMinute(Date date) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.set(Calendar.SECOND, 0);
		gc.set(Calendar.MILLISECOND, 0);
		return gc.getTime();
	}

}
